package org.midi.j2me.ebook;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.Vector;

public class DataRes {

	private static DataRes instance;

	private Vector chapters;

	private boolean debug = true;

	private DataRes() {
		chapters = new Vector();
		// if(debug){
		// try {
		// // getChapters();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }
	}

	public static DataRes getInstance() {
		if (instance == null) {
			instance = new DataRes();

		}
		return instance;
	}

	public Vector getChapters() throws IOException {
		InputStream is = this.getClass().getResourceAsStream("0");
		DataInputStream dis = new DataInputStream(is);
		String chapter = null;
		while ((chapter = dis.readUTF()) != null) {
			chapters.addElement(chapter);
			if (debug) {
				System.out.println(chapter);
			}
		}

		return chapters;
	}

	public String getChapter(int chapterIndex) {

		InputStream is = this.getClass().getResourceAsStream(
				"/config/" + (chapterIndex + 1));
		byte[] buf = new byte[60 * 1024];
		int len = 0;
		try {
			len=is.read(buf);
		} catch (IOException e) {		
			e.printStackTrace();
		}

		byte[] result = new byte[len];
		System.arraycopy(buf, 0, result, 0, len);

		String r=null;
		try {
			r =new String(result,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		return r;
	}

}
